package com.zsoft.test;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.zsoft.test.HouseEvent;

public enum Dao {
	INSTANCE;

	public List<HouseEvent> listEvents() {
		EntityManager em = EMFService.get().createEntityManager();
		// Read the existing entries
		Query q = em.createQuery("select m from HouseEvent m");
		List<HouseEvent> events = q.getResultList();
		return events;
	}

	public void add(String sender, String desc, Date date) {
		synchronized (this) {
			EntityManager em = EMFService.get().createEntityManager();
			HouseEvent event = new HouseEvent(sender, desc, date);
			em.persist(event);
			em.close();
		}
	}
/*
	public List<HouseEvent> getTodos(String userId) {
		EntityManager em = EMFService.get().createEntityManager();
		Query q = em
				.createQuery("select t from HouseEvents t where t.author = :userId");
		q.setParameter("userId", userId);
		List<HouseEvent> todos = q.getResultList();
		return todos;
	}
*/
	
	public void remove(long id) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			HouseEvent event = em.find(HouseEvent.class, id);
			em.remove(event);
		} finally {
			em.close();
		}
	}
	
	
	
	public List<HouseEvent> getTestEvents() {
		List<HouseEvent> events = new ArrayList<HouseEvent>();
		events.add(new HouseEvent("test", "test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		events.add(new HouseEvent("test2", "mera test", new Date()));
		return events;
	}

}
